﻿// <copyright file="EndGameMenu.cs" company="ThorIndustry">Copyright (c) ThorIndustry. All rights reserved.</copyright>

using System.Collections.Generic;

public class EndGameMenu : Menu
{
    public UnityEngine.GameObject LabelPrefab;
    public UnityEngine.GameObject BackgroundPrefab;
    private IViewService viewService;

    private UnityEngine.GUIText winLabel;
    private UnityEngine.GUITexture winBackground;
    
    public override void Initialize(IGuiController guiService)
    {
        base.Initialize(guiService);

        this.viewService = Services.GetService<IViewService>();

        this.InitializeMenu();
        this.Hide();
    }

    public override void Hide()
    {
        base.Hide();

        this.winLabel.gameObject.SetActive(false);
        this.winBackground.gameObject.SetActive(false);
    }

    public override void Show(params object[] parameters)
    {
        base.Show(parameters);

        this.winLabel.gameObject.SetActive(true);
        this.winBackground.gameObject.SetActive(true);
    }

    private void InitializeMenu()
    {
        float centerY = UnityEngine.Screen.height / 2;

        float x = 0.5f;

        // Background.
        UnityEngine.GameObject backgroundObject = UnityEngine.GameObject.Instantiate(this.BackgroundPrefab) as UnityEngine.GameObject;
        this.winBackground = backgroundObject.GetComponent<UnityEngine.GUITexture>();

        UnityEngine.Rect rect = new UnityEngine.Rect();
        rect.height = 150 * GuiHelper.GetScreenSizeRatio();
        rect.width = UnityEngine.Screen.width;
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;

        this.winBackground.pixelInset = rect;

        UnityEngine.Transform backgroundTransform = backgroundObject.transform;
        backgroundTransform.localPosition = new UnityEngine.Vector3(x, 0.5f, -1);

        // Win label...
        UnityEngine.GameObject labelObject = UnityEngine.GameObject.Instantiate(this.LabelPrefab) as UnityEngine.GameObject;
        this.winLabel = labelObject.GetComponent<UnityEngine.GUIText>();

        this.winLabel.text = "Puzzle Terminé !";
        this.winLabel.fontSize = UnityEngine.Mathf.RoundToInt(24 * GuiHelper.GetScreenSizeRatio());
        UnityEngine.Transform labelTransform = labelObject.transform;
        labelTransform.localPosition = new UnityEngine.Vector3(x, 0.5f, 0);

        // Back button.
        UnityEngine.GameObject buttonObject = UnityEngine.GameObject.Instantiate(this.HomePrefab) as UnityEngine.GameObject;
        Button currentButton = buttonObject.GetComponent<Button>();
        this.buttons.Add(currentButton);
        currentButton.IsVisible = this.IsVisible;
        currentButton.Clicked += this.ButtonBackToMain_Clicked;

        // Place the button.
        UnityEngine.Transform buttonTransform = currentButton.transform;
        buttonTransform.parent = this.transform;

        rect.height = 64 * GuiHelper.GetScreenSizeRatio();
        rect.width = 64 * GuiHelper.GetScreenSizeRatio();
        rect.x = -rect.width / 2;
        rect.y = -rect.height / 2;
        currentButton.GuiTexture.pixelInset = rect;
        buttonTransform.localPosition = new UnityEngine.Vector3(x, 0.2f, 0);

        currentButton.GuiText.pixelOffset = new UnityEngine.Vector2(0, 0);
    }

    private void ButtonBackToMain_Clicked(object sender, System.EventArgs args)
    {
        this.GuiController.BackMenu();

        // this.viewService.Activate(typeof(OutGameView));
        this.viewService.Activate(typeof(WelcomeView));
    }
}

